package top.ljlgithub.api.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ljlgithub.api.bean.Cat;
import top.ljlgithub.api.bean.CatPageBean;
import top.ljlgithub.api.bean.PageCondition;
import top.ljlgithub.api.bean.TreeCat;
import top.ljlgithub.api.mapper.CatMapper;
import top.ljlgithub.api.service.CatService;
import top.ljlgithub.api.vo.CatParam;

import java.util.*;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 7:29
 * @Version 1.0
 **/
@Service
public class CatServiceImpl implements CatService {
    @Autowired
    CatMapper catMapper;

    @Override
    public List<Cat> getCateList(CatParam catParam) {
        List<Cat> cateList;
        //如果有page参数
        if (catParam.getPagesize() != null && catParam.getPagenum() != null) {
            Integer pagesize = catParam.getPagesize();
            Integer pagenum = catParam.getPagenum();
            Integer startIndex = (pagenum - 1) * pagesize;
            cateList = catMapper.getCateList(startIndex, pagesize);
        } else {
            cateList = catMapper.getCateListByType(catParam.getType());
        }
        //如果只有type参数
        return cateList;
    }

    @Override
    public Cat findCatById(Integer id) {
        Cat cat = catMapper.findCateById(id);
        return cat;
    }

    @Override
    public void deleteCateById(Integer id) {
        catMapper.deleteCateById(id);
    }

    @Override
    public Cat updateCate(Integer id, String cat_name) {
        catMapper.updateCate(id, cat_name);
        Cat cat = catMapper.findCateById(id);
        return cat;
    }

    @Override
    public Cat addCate(Cat cat) {
        catMapper.addCate(cat);
        Cat cate = catMapper.findCateById(cat.getCat_id());
        return cate;
    }

    @Override
    public Object getCateTree(CatParam catParam) {
        List<Cat> nodeList = getCateList(catParam);
        if (catParam.getPagenum() != null && catParam.getPagesize() != null) {
            //如果携带page参数，nodeList是一级分类的分页结果
            //然后根据一级分类来获取二级分类等
            Integer type = catParam.getType();
            if (type > 1) {
                Integer oneLength = nodeList.size();
                for (int i = 0; i < oneLength; i++) {
                    Integer cat_id = nodeList.get(i).getCat_id();
                    List<Cat> childCateList = catMapper.getChildCateList(cat_id);
                    if (type > 2) {
                        Integer twoLength = childCateList.size();
                        for (int j = 0; j < twoLength; j++) {
                            Integer cat_id1 = childCateList.get(j).getCat_id();
                            List<Cat> childCateList1 = catMapper.getChildCateList(cat_id1);
                            childCateList.addAll(childCateList1);
                        }
                    }
                    nodeList.addAll(childCateList);
                }
            }
        }
        List<TreeCat> treeList = new ArrayList<>();
        Map<Integer, List<Cat>> nodeLevelMap = new HashMap<>();
        nodeList.forEach(node -> {
            Integer level = node.getCat_level();
            if (nodeLevelMap.containsKey(level)) {
                nodeLevelMap.get(level).add(node);
            } else {
                List<Cat> testCaseNodes = new ArrayList<>();
                testCaseNodes.add(node);
                nodeLevelMap.put(node.getCat_level(), testCaseNodes);
            }
        });
        List<Cat> rootNodes = Optional.ofNullable(nodeLevelMap.get(0)).orElse(new ArrayList<>());
        rootNodes.forEach(rootNode -> treeList.add(buildNodeTree(nodeLevelMap, rootNode)));
        if (catParam.getPagenum() != null && catParam.getPagesize() != null){
            CatPageBean catPageBean = handlePage(catParam, treeList);
            return catPageBean;
        }
        return treeList;
    }

    /**
     * 递归构建节点树
     *
     * @param nodeLevelMap
     * @param rootNode
     * @return
     */
    private TreeCat buildNodeTree(Map<Integer, List<Cat>> nodeLevelMap, Cat rootNode) {

        TreeCat nodeTree = new TreeCat();
        BeanUtils.copyProperties(rootNode, nodeTree);

        List<Cat> lowerNodes = nodeLevelMap.get(rootNode.getCat_level() + 1);
        if (lowerNodes == null) {
            return nodeTree;
        }

        List<TreeCat> children = Optional.ofNullable(nodeTree.getChildren()).orElse(new ArrayList<>());

        lowerNodes.forEach(node -> {
            if (node.getCat_pid() != null && node.getCat_pid().equals(rootNode.getCat_id())) {
                children.add(buildNodeTree(nodeLevelMap, node));
                nodeTree.setChildren(children);
            }
        });
        return nodeTree;
    }

    private CatPageBean handlePage(CatParam catParam, List<TreeCat> treeList) {
        CatPageBean catPageBean = new CatPageBean();
        catPageBean.setPagenum(catParam.getPagenum());
        catPageBean.setPagesize(catParam.getPagesize());
        catPageBean.setTotal(catMapper.getTotal());
        catPageBean.setResult(treeList);
        return catPageBean;
    }

}
